package cn.zucc.edu.madq.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.util.JSONPObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zucc.edu.madq.service.ClubService;
import cn.zucc.edu.madq.pojo.Club;
import cn.zucc.edu.madq.pojo.User;

@Controller
@RequestMapping("club")
public class ClubController {
	@Autowired
	private ClubService clubService;

	private static Logger logger = Logger.getLogger(ClubController.class);

	@RequestMapping("updateClub")
	@ResponseBody
	public JSONPObject updateClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response,
			String clubAbout, String clubName, int clubId) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			if (user.getUserId() == clubService.getClubById(clubId)
					.getClubBoss()) {
				Club c = new Club();
				c.setClubAbout(clubAbout);
				c.setClubName(clubName);
				c.setClubId(clubId);
				c.setClubBoss(user.getUserId());
				count = clubService.updateClub(c);
			}
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}

	@RequestMapping("delBossClub")
	@ResponseBody
	public JSONPObject delBossClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("clubId") int clubId, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			if (user.getUserId() == clubService.getClubById(clubId)
					.getClubBoss()) {
				count = clubService.delBossClub(clubId);
			}
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			// throw e;
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("getBossClub")
	@ResponseBody
	public JSONPObject getBossClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> map = clubService.getBossClub(user
					.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, map);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			// throw e;
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("getManagerClub")
	@ResponseBody
	public JSONPObject getManagerClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> map = clubService.getManagerClub(user
					.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, map);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			// throw e;
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("addUserClub")
	@ResponseBody
	public JSONPObject addUserClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("clubId") int clubId, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			count = clubService.addUserClub(clubId, user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			throw e;
			// return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("delUserClub")
	@ResponseBody
	public JSONPObject delUserClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("userClubId") int userClubId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		int count = 0;
		try {
			count = clubService.delUserClub(userClubId);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			// throw e;
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("getAllClub")
	@ResponseBody
	public JSONPObject getAllClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		try {
			List<Club> clubList = clubService.getAllClub();
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, clubList);
		} catch (Exception e) {
			int count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("getMyClub")
	@ResponseBody
	public JSONPObject getMyClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> map = clubService.getUserClub(user
					.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, map);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			// throw e;
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("getNotMyClub")
	@ResponseBody
	public JSONPObject getNotMyClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		try {
			List<Map<String, Object>> map = clubService
					.getNotUserClub(user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, map);
		} catch (Exception e) {
			int count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			// throw e;
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("getClub")
	@ResponseBody
	public JSONPObject getClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("clubId") int clubId, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		int count = 0;
		try {
			Club club = clubService.getClubById(clubId);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, club);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			// throw e;
			return new JSONPObject(jsonpCallback, count);
		}
	}

	@RequestMapping("regClub")
	@ResponseBody
	public JSONPObject regClub(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("clubAbout") String clubAbout,
			@RequestParam("clubName") String clubName,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------start------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		int count = 0;
		Club club = new Club();
		club.setClubAbout(clubAbout);
		club.setClubName(clubName);
		club.setClubRegistedate(new Date());
		try {
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				count = -2;
				return new JSONPObject(jsonpCallback, count);
			}
			club.setClubBoss(user.getUserId());
			count = clubService.regClub(club);
		} catch (Exception e) {
			count = -1;
			logger.error(e.getMessage(), e);
		}
		logger.info("------end------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		return new JSONPObject(jsonpCallback, count);
	}
}
